/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.manageplugins;

import gui.Factory;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
import kernel.InstallRepositoryPlugin;
import kernel.XMLPluginConfiguration;
import kernel.Plugin;
import org.jdom.Element;

class MyButtonCell extends AbstractCellEditor
        implements TableCellRenderer, TableCellEditor, ActionListener {

    JTable table;
    JButton renderButton;
    JButton editButton;
    String text;
    private ManagePlugins list;
    private String s = File.separator;
    private String language_xml;

    private String cancel, install, clean, remove;


    public MyButtonCell(JTable table, int column, ManagePlugins list) {
        super();
        language_xml = Factory.getWorkspace() + s + "files" + s + "language.xml";
        this.table = table;
        renderButton = new JButton();

        editButton = new JButton();
        editButton.setFocusPainted(true);
        //editButton.addActionListener(this);
        editButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                ActionPerformed(e);
            }

        });

        TableColumnModel columnModel = table.getColumnModel();
        columnModel.getColumn(column).setCellRenderer(this);
        columnModel.getColumn(column).setCellEditor(this);
        this.list = list;
        loadLanguage();
    }

    private void loadLanguage(String language){
        XMLPluginConfiguration xml = new XMLPluginConfiguration(language_xml);
        //menu texts
        clean = xml.getTag("/languages/"+language+"/windows/manage_plugins/button_text/clean");
        remove = xml.getTag("/languages/"+language+"/windows/manage_plugins/button_text/remove");
        install = xml.getTag("/languages/"+language+"/windows/manage_plugins/button_text/install");
        cancel = xml.getTag("/languages/"+language+"/windows/manage_plugins/button_text/cancel");
        System.out.println("new install label should be: "+this.install);
    }

    protected void loadLanguage(){
        XMLPluginConfiguration xml = new XMLPluginConfiguration(language_xml);
        String lang = xml.getDoc().getRootElement().getAttributeValue("selected");
        loadLanguage(lang);
    }

    public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
        if (hasFocus) {
            renderButton.setForeground(table.getForeground());
            renderButton.setBackground(UIManager.getColor(
                    "Button.background"));
        } else if (isSelected) {
            renderButton.setForeground(table.getSelectionForeground());
            renderButton.setBackground(table.getSelectionBackground());
        } else {
            renderButton.setForeground(table.getForeground());
            renderButton.setBackground(UIManager.getColor(
                    "Button.background"));
        }
        //   value = "Borrar";
        renderButton.setText((value == null) ? "" : value.toString());
        return renderButton;
    }

    public Component getTableCellEditorComponent(
            JTable table, Object value, boolean isSelected, int row,
            int column) {
        //   value = "Borrar";
        text = (value == null) ? "" : value.toString();
        System.out.println("MyButtonCell text value: "+text);
        editButton.setText(text);
        return editButton;
    }

    public Object getCellEditorValue() {
        return text;
    }

    public void removeAction() {
        //borrar plugin
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        String plug = (String) model.getValueAt(table.getSelectedRow(),
                0);

        try {
            /*if (kernel.PluginOperations.removePlugin(list.selectedModule(), plug) != null) {
                list.selectModule(list.selectedModule(), false);
            } else {
                list.reloadComboBoxOptions(1);
            }*/
            Factory.makeGUISingleton().loadPluginsIcons();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void installAction() {
        int row = table.getSelectedRow();
        //instalar plugin
        //Plugin p = listaPlugins.get(table.getSelectedRow());
        Vector<Plugin> listaPlugins = Factory.makeManagePluginsSingleton().getListaPlugins();
        Plugin p = listaPlugins.get(row);

        listaPlugins.remove(p);
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.removeRow(row);

        /* */
        /* */

        ManagePlugins mana = Factory.makeManagePluginsSingleton();

        final InstallRepositoryPlugin install = new InstallRepositoryPlugin(p);

        /* necesario que se ejecute en otro hilo */
        Thread t = new Thread(new Runnable() {

            public void run() {
                install.exec();
            }
        });
        install.setDownload(mana.addNewDownload(t));
        
        t.start();
        
    }

    public void cancelDownload(int row) {
        //remove row
        ((DefaultTableModel) table.getModel()).removeRow(row);
        Factory.makeManagePluginsSingleton().stopDownloadAtRow(row);
    }

    public void cleanDownload(int row) {
        //remove row
        ((DefaultTableModel) table.getModel()).removeRow(row);
        Factory.makeManagePluginsSingleton().cleanDownloadAtRow(row);
    }

    public void ActionPerformed(java.awt.event.ActionEvent evt) {
        fireEditingStopped();
        //System.out.println("Button text: "+((JButton) evt.getSource()).getText());
        //System.out.println( evt.getActionCommand() + " : " + table.getSelectedRow());
        if (((JButton) evt.getSource()).getText().equals(remove)) {
            removeAction();
        } else if (((JButton) evt.getSource()).getText().equals(install)) {
            installAction();
        } else if (((JButton) evt.getSource()).getText().equals(cancel)) {
            cancelDownload(table.getSelectedRow());
        } else if (((JButton) evt.getSource()).getText().equals(clean)) {
            cleanDownload(table.getSelectedRow());
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }
}

